package com.czd.support.service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.czd.base.model.Paginate;
import com.czd.base.service.BaseService;
import com.czd.support.model.User;

/**
 * Created with IntelliJ IDEA.
 * Author: GuSi
 * Create: GuSi (14-7-7 12:42)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Service
public class UserService extends BaseService<User> {
	public boolean insert(User user ) {
		return this.insert(sqlId("insertuser"), user);
	}
	public User findByUser(User user ) {
		return this.selectOne(sqlId("findByUser"), user);
	}
	/**
	 * 根据 输入的accountCode 获取账户信息
	 * @param accountCode
	 * @return
	 */
	public User findByAccountCode(String accountCode) {
		return this.selectOne(sqlId("findByAccountCode"),accountCode);
	}


	/**
	 * 根据帐号id 获取用户信息
	 * @param id
	 * @return
	 */
	public User findById(int id) {
		return this.selectOne(sqlId("findById"),id);
	}
	
	/**
	 * 根据关键字 获取用户列表
	 * @param id
	 * @return
	 */
	public List<User> findByName(String name) {
		Map<String,Object> m = new HashMap<>();
		m.put("name",name);
		return this.selectList(sqlId("findByName"),m);
	}



	/**
	 * 修改头像地址
	 * @param url 头像地址
	 * @param id 员工id
	 */
	public boolean updateAvatar(String url, int id) {
		Map<String,Object> map = new HashMap<>();
		map.put("url",url);
		map.put("id",id);
		return this.update(sqlId("updateAvatar"),map);
	}

	/**
	 *
	 * @return
	 */
	public Paginate queryForPage(User user,String kw) {
        user.setWhere(" 1=1");
        if(StringUtils.isNotBlank(kw)){
            user.setWhere(user.getWhere() + " and (mobilePhone like '"+kw+"%' or mail like '"+kw+"%' or nickName like '"+kw+"%')");
        }
        if(user.getStatus() >= 0){
            user.setWhere(user.getWhere() +" and c.cStatus = "+user.getStatus());
        }
		return this.queryForPaginate(sqlId("queryForPage"), user);
	}

	public User findById2(Integer accountId) {
		return this.selectOne(sqlId("findById2"),accountId);
	}

	/**
	 * 根据员工帐号密码，获取员工对象
	 * @return 员工对象
	 */
	public User login(String accountCode, String password){
		Map<String,Object> m = new HashMap<>();
		m.put("accountCode",accountCode);
		m.put("password",password);
		return this.selectOne(sqlId("login"),m);
	}
	
	/**
	 * 用户注册
	 * @param user
	 * @return
	 */
	public boolean register(User user){
		return this.insert(sqlId("registerAccount"), user);
	}
	
	/**
	 * 重置帐号密码
	 * @param id
	 * @param s
	 * @return
	 */
	public boolean resetPwd(int id, String s) {
		Map<String,Object> m = new HashMap<>();
		m.put("id",id);
		m.put("password",s);
		return this.update(sqlId("resetPwd"), m);
	}
	
	/**
	 * 修改密码
	 * @param id
	 * @param password
	 * @return
	 */
	public boolean modifyPwd(int id, String password) {
		return resetPwd(id,password);
	}
	
	/**
	 * 校验手机号码是否注册过  
	 */
	public List<User>  findByPhone(User  user){
		return  this.selectList("findByPhone", user);
		
	}
	/**
	 * 校验手机号码是否注册过  
	 */
	public List<User>  findByMail(User  user){
		return  this.selectList("findByMail", user);
		
	}
	
	/**
	 * 用户列表
	 * @param user
	 * @param kw
	 * @return
	 */
	public Paginate queryPageForUser(User user,String kw) {
		user.setWhere(" 1=1");
        if(StringUtils.isNotBlank(kw)){
        	user.setWhere(user.getWhere() + " and (u2.mobilePhone like '"+kw+"%' or u2.mail like '"+kw+"%' or u2.inviteCode = '"+kw+"')");
        }
        return this.queryForPaginate(sqlId("queryPageForUser"), "userListCount", user);
	}
	
	/**
	 * 根据条件查询代理商
	 * @param user
	 * @param kw
	 * @return
	 */
	public User findByKw(User user,String kw){
		user.setWhere(" 1=1 ");
        if(StringUtils.isNotBlank(kw)){
        	user.setWhere(user.getWhere() + " and (mobilePhone like '"+kw+"%' or mail like '"+kw+"%' or inviteCode = '"+kw+"')");
        }
        return this.selectOne(sqlId("findByKw"), user);
	}
}
